# 基本图形生成工具 - 设置和运行脚本
# 此脚本会创建虚拟环境、安装依赖并运行图形生成工具

Write-Host "基本图形生成工具 - 设置和运行脚本" -ForegroundColor Green
Write-Host "======================================" -ForegroundColor Green
Write-Host ""

# 检查 Python 是否已安装
try {
    $pythonVersion = python --version 2>&1
    Write-Host "检测到 Python: $pythonVersion" -ForegroundColor Green
} catch {
    Write-Host "错误: 未找到 Python，请先安装 Python 3.6 或更高版本" -ForegroundColor Red
    Write-Host "可以从 https://www.python.org/downloads/ 下载安装" -ForegroundColor Yellow
    Read-Host "按任意键退出"
    exit 1
}

# 设置虚拟环境名称
$venvName = "shape_generator_env"
$venvPath = Join-Path $PSScriptRoot $venvName

# 检查虚拟环境是否已存在
if (Test-Path $venvPath) {
    Write-Host "虚拟环境 '$venvName' 已存在" -ForegroundColor Yellow
    $choice = Read-Host "是否要重新创建虚拟环境? (y/N)"
    if ($choice -eq 'y' -or $choice -eq 'Y') {
        Write-Host "删除现有虚拟环境..." -ForegroundColor Yellow
        Remove-Item -Path $venvPath -Recurse -Force
    } else {
        Write-Host "使用现有虚拟环境" -ForegroundColor Green
    }
}

# 创建虚拟环境（如果不存在）
if (-not (Test-Path $venvPath)) {
    Write-Host "创建虚拟环境 '$venvName'..." -ForegroundColor Green
    try {
        python -m venv $venvPath
        Write-Host "虚拟环境创建成功" -ForegroundColor Green
    } catch {
        Write-Host "错误: 创建虚拟环境失败" -ForegroundColor Red
        Read-Host "按任意键退出"
        exit 1
    }
}

# 激活虚拟环境
Write-Host "激活虚拟环境..." -ForegroundColor Green
$activateScript = Join-Path $venvPath "Scripts\Activate.ps1"

# 检查 PowerShell 执行策略
$executionPolicy = Get-ExecutionPolicy
if ($executionPolicy -eq "Restricted") {
    Write-Host "警告: PowerShell 执行策略为 Restricted，正在更改为 RemoteSigned" -ForegroundColor Yellow
    try {
        Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
        Write-Host "执行策略已更改为 RemoteSigned" -ForegroundColor Green
    } catch {
        Write-Host "错误: 无法更改执行策略" -ForegroundColor Red
        Write-Host "请手动运行: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser" -ForegroundColor Yellow
        Read-Host "按任意键退出"
        exit 1
    }
}

# 激活虚拟环境
try {
    & $activateScript
    Write-Host "虚拟环境已激活" -ForegroundColor Green
} catch {
    Write-Host "错误: 无法激活虚拟环境" -ForegroundColor Red
    Read-Host "按任意键退出"
    exit 1
}

# 询问用户是否升级 pip
$upgradePip = Read-Host "是否要升级 pip? (y/N)"
if ($upgradePip -eq 'y' -or $upgradePip -eq 'Y') {
    Write-Host "升级 pip..." -ForegroundColor Green
    try {
        python -m pip install --upgrade pip
        Write-Host "pip 升级完成" -ForegroundColor Green
    } catch {
        Write-Host "警告: pip 升级失败，继续使用现有版本" -ForegroundColor Yellow
    }
} else {
    Write-Host "跳过 pip 升级" -ForegroundColor Yellow
}

# 询问用户是否安装依赖
$installDeps = Read-Host "是否要安装依赖? (y/N)"
if ($installDeps -eq 'y' -or $installDeps -eq 'Y') {
    # 检查 requirements.txt 是否存在
    $requirementsPath = Join-Path $PSScriptRoot "requirements.txt"
    if (Test-Path $requirementsPath) {
        Write-Host "从 requirements.txt 安装依赖..." -ForegroundColor Green
        try {
            pip install -r $requirementsPath
            Write-Host "依赖安装完成" -ForegroundColor Green
        } catch {
            Write-Host "错误: 依赖安装失败" -ForegroundColor Red
            Read-Host "按任意键退出"
            exit 1
        }
    } else {
        Write-Host "未找到 requirements.txt，手动安装 PySide6..." -ForegroundColor Yellow
        try {
            pip install PySide6
            Write-Host "PySide6 安装完成" -ForegroundColor Green
        } catch {
            Write-Host "错误: PySide6 安装失败" -ForegroundColor Red
            Read-Host "按任意键退出"
            exit 1
        }
    }
} else {
    Write-Host "跳过依赖安装" -ForegroundColor Yellow
}

# 检查主脚本是否存在
$mainScript = Join-Path $PSScriptRoot "shape_generator.py"
if (Test-Path $mainScript) {
    Write-Host "" -ForegroundColor Green
    Write-Host "所有设置完成，正在启动图形生成工具..." -ForegroundColor Green
    Write-Host "" -ForegroundColor Green
    
    # 运行主脚本
    try {
        python $mainScript
    } catch {
        Write-Host "错误: 运行主脚本失败" -ForegroundColor Red
        Read-Host "按任意键退出"
        exit 1
    }
} else {
    Write-Host "错误: 未找到主脚本 shape_generator.py" -ForegroundColor Red
    Read-Host "按任意键退出"
    exit 1
}

Write-Host "程序已退出" -ForegroundColor Green